<script setup lang="ts">
import { getUserInfoById } from '@/api/user';
import { User } from '@/api/user/type';
import { useRouter, useRoute } from 'vue-router';
import { onMounted, ref, computed } from 'vue';
import { useUserStore } from '@/store/user';
import { showToast } from 'vant';

// 扩展User类型，确保包含页面中使用的所有字段
interface ExtendedUser extends User {
  signature?: string;
  email?: string;
  school?: string;
  major?: string;
  enrollmentYear?: string;
  createTime?: string;
}

const router = useRouter();
const route = useRoute();
const userStore = useUserStore();
const userInfo = ref({} as ExtendedUser);
const isCurrentUser = computed(() => !route.query.id || route.query.id === userStore.user.id);
const loading = ref(true);

onMounted(async () => {
  try {
    const id = route.query.id || userStore.user.id;
    
    if (id) {
      loading.value = true;
      const res = await getUserInfoById(id.toString());
      userInfo.value = res.data as ExtendedUser;
    } else {
      // 如果没有ID，尝试使用当前用户的信息
      userInfo.value = { ...userStore.user } as ExtendedUser;
    }
  } catch (error) {
    console.error('获取用户信息失败', error);
    showToast('获取用户信息失败');
  } finally {
    loading.value = false;
  }
});

// 明确指定参数类型
const toEditPage = (field: string) => {
  if (!isCurrentUser.value) return;
  
  router.push({
    name: "Nickname",
    query: {
      'userId': userInfo.value.id,
      'field': field
    }
  });
};

// 处理图片加载错误的函数
const handleImageError = (e: Event) => {
  const target = e.target as HTMLImageElement;
  if (target) {
    target.src = 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg';
  }
};
</script>

<template>
    <div class="user-profile-container">
        <div v-if="loading" class="loading-container">
            <van-loading type="spinner" color="#1989fa" />
            <span>加载用户信息中...</span>
        </div>
        
        <template v-else>
            <!-- 个人资料卡片 -->
            <div class="profile-card content-card">
                <div class="profile-header">
                    <h2 class="page-title">个人资料</h2>
                </div>
                
                <div class="profile-content">
                    <div class="avatar-container">
                        <img 
                            :src="userInfo.avatar" 
                            alt="用户头像" 
                            class="user-avatar-enhanced"
                            @error="handleImageError"
                        />
                        <van-button 
                            v-if="isCurrentUser" 
                            class="change-avatar-btn" 
                            type="primary" 
                            size="mini" 
                            icon="photograph"
                            @click="toEditPage('avatar')"
                        >
                            更换头像
                        </van-button>
                    </div>
                    
                    <div class="user-info">
                        <div class="info-item">
                            <span class="label">昵称</span>
                            <span class="value">{{ userInfo.nickname || '未设置' }}</span>
                            <van-icon v-if="isCurrentUser" name="arrow" class="edit-icon" @click="toEditPage('nickname')" />
                        </div>
                        
                        <div class="info-item">
                            <span class="label">性别</span>
                            <span class="value">
                                <van-icon v-if="userInfo.sex === '男'" name="contact" class="gender-icon male" />
                                <van-icon v-else-if="userInfo.sex === '女'" name="like" class="gender-icon female" />
                                {{ userInfo.sex || '未设置' }}
                            </span>
                            <van-icon v-if="isCurrentUser" name="arrow" class="edit-icon" @click="toEditPage('sex')" />
                        </div>
                        
                        <div class="info-item">
                            <span class="label">个性签名</span>
                            <span class="value signature">{{ userInfo.signature || '这个人很懒，什么都没有留下' }}</span>
                            <van-icon v-if="isCurrentUser" name="arrow" class="edit-icon" @click="toEditPage('signature')" />
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- 教育信息卡片 -->
            <div class="education-card content-card">
                <div class="card-header">
                    <h3>教育信息</h3>
                    <van-button v-if="isCurrentUser" type="primary" size="mini" icon="edit" @click="toEditPage('education')" plain>编辑</van-button>
                </div>
                
                <div class="card-content">
                    <div class="info-item">
                        <span class="label">学校</span>
                        <span class="value">{{ userInfo.school || '未设置' }}</span>
                    </div>
                    
                    <div class="info-item">
                        <span class="label">专业</span>
                        <span class="value">{{ userInfo.major || '未设置' }}</span>
                    </div>
                    
                    <div class="info-item">
                        <span class="label">入学年份</span>
                        <span class="value">{{ userInfo.enrollmentYear || '未设置' }}</span>
                    </div>
                </div>
            </div>
            
            <!-- 账号信息卡片 -->
            <div class="account-card content-card">
                <div class="card-header">
                    <h3>账号信息</h3>
                </div>
                
                <div class="card-content">
                    <div class="info-item">
                        <span class="label">用户名</span>
                        <span class="value">{{ userInfo.username }}</span>
                    </div>
                    
                    <div class="info-item">
                        <span class="label">手机号</span>
                        <span class="value">{{ userInfo.phone || '未设置' }}</span>
                    </div>
                    
                    <div class="info-item">
                        <span class="label">账号ID</span>
                        <span class="value">{{ userInfo.id }}</span>
                    </div>
                </div>
            </div>
        </template>
    </div>
</template>

<style lang="scss" scoped>
.user-profile-container {
    max-width: 100%;
    padding: 16px;
    
    .loading-container {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        height: 200px;
        gap: 16px;
        
        span {
            color: #666;
        }
    }
    
    .profile-card {
        margin-bottom: 24px;
        
        .profile-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 24px;
            
            .page-title {
                margin: 0;
            }
        }
        
        .profile-content {
            display: flex;
            gap: 24px;
            
            @media (max-width: 768px) {
                flex-direction: column;
                align-items: center;
            }
            
            .avatar-container {
                display: flex;
                flex-direction: column;
                align-items: center;
                gap: 8px;
                
                img {
                    width: 120px;
                    height: 120px;
                    border-radius: 50%;
                    object-fit: cover;
                    border: 3px solid #fff;
                    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
                }
                
                .change-avatar-btn {
                    margin-top: 8px;
                }
            }
            
            .user-info {
                flex: 1;
                padding-top: 8px;
            }
        }
    }
    
    .content-card {
        background-color: #fff;
        border-radius: 8px;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
        padding: 24px;
        margin-bottom: 24px;
        transition: all 0.3s ease;
        
        &:hover {
            box-shadow: 0 3px 8px rgba(0, 0, 0, 0.15);
        }
        
        .card-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 16px;
            
            h3 {
                font-size: 18px;
                font-weight: 600;
                color: #333;
                margin: 0;
            }
        }
        
        .card-content {
            padding: 0 8px;
        }
    }
    
    .info-item {
        display: flex;
        padding: 12px 0;
        border-bottom: 1px solid #f5f5f5;
        align-items: center;
        
        &:last-child {
            border-bottom: none;
        }
        
        .label {
            width: 80px;
            color: #666;
            font-size: 14px;
        }
        
        .value {
            flex: 1;
            color: #333;
            display: flex;
            align-items: center;
            
            .gender-icon {
                margin-right: 4px;
                
                &.male {
                    color: #1989fa;
                }
                
                &.female {
                    color: #ff2c7d;
                }
            }
            
            &.signature {
                color: #666;
                font-style: italic;
            }
        }
        
        .edit-icon {
            margin-left: 8px;
            color: #999;
            cursor: pointer;
            
            &:hover {
                color: #1989fa;
            }
        }
    }
}
</style>